package user

import (
	"fmt"
	"gin_miaosa/zhiliao_web/utils"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/go-micro/plugins/v4/client/grpc"
	"go-micro.dev/v4"

	// "github.com/micro/go-grpc"
	// "github.com/asim/go-micro/plugins/server/grpc/v4"
	"context"
	zhiliao_user_srv "gin_miaosa/zhiliao_user_srv/proto/user_front"
)



func SendEmail(ctx *gin.Context)  {

	email := ctx.PostForm("email")
	is_ok := utils.VerifyEmail(email)
	

	if is_ok{ // 位true则进行grpc通信

		// grpc 通讯
		service := micro.NewService(
			micro.Client(grpc.NewClient()),
		)
		service.Init()
		client := zhiliao_user_srv.NewFrontUserService("zhiliao.user.srv.zhiliao_user_srv",service.Client())
		rep, _ := client.FrontUserSendEmail(context.TODO(),&zhiliao_user_srv.FrontUserMailRequest{Email:email})


		// rpc 版本
		// consulReg := consul.NewRegistry(
		// 	//指定微服务的ip:  选择注册服务器地址,默认为本机,也可以选择consul集群中的client
		// 	registry.Addrs("127.0.0.1:8500"),
		// )
		// // Create service
		// srv := micro.NewService(
		// 	//注册consul
		// 	micro.Registry(consulReg),
		// )
		
		// srv := micro.NewService()
		// srv.Init()
		// c := zhiliao_user_srv.NewFrontUserService("zhiliao.user.srv.zhiliao_user_srv", srv.Client())
		// rep, _ := c.FrontUserSendEmail(context.TODO(),&zhiliao_user_srv.FrontUserMailRequest{Email:email})
			
		
		

		ctx.JSON(http.StatusOK,gin.H{
			"code":rep.Code,
			"msg":rep.Msg,
		})

	}else {
		// fmt.Println("err email",email)

		ctx.JSON(http.StatusOK,gin.H{
			"code":500,
			"msg":"邮箱格式不正确",
		})
	}

}

func FrontUserRegister(ctx *gin.Context)  {

	email := ctx.PostForm("email")
	is_ok := utils.VerifyEmail(email)
	if is_ok{
		captche := ctx.PostForm("captche")
		password := ctx.PostForm("password")
		repassword := ctx.PostForm("repassword")

		if password != repassword {
			ctx.JSON(http.StatusOK,gin.H{
				"code":200,
				"msg":"两次密码不一致",
			})
		}else { // 所有都没问题
			// grpc通信
			service := micro.NewService(
				micro.Client(grpc.NewClient()),
			)
			service.Init()
			client := zhiliao_user_srv.NewFrontUserService("zhiliao.user.srv.zhiliao_user_srv",service.Client())
			rep, err := client.FrontUserRegister(context.TODO(),&zhiliao_user_srv.FrontUserRequest{Email:email,Code:captche,Password:password,Reassword:repassword})
	
	
			// consulReg := consul.NewRegistry(
			// 	//指定微服务的ip:  选择注册服务器地址,默认为本机,也可以选择consul集群中的client
			// 	registry.Addrs("127.0.0.1:8500"),
			// )
			// // Create service
			// srv := micro.NewService(
			// 	//注册consul
			// 	micro.Registry(consulReg),
			// )
			// c := zhiliao_user_srv.NewFrontUserService("zhiliao.user.srv.zhiliao_user_srv", srv.Client())
			// rep, err := c.FrontUserRegister(context.TODO(),&zhiliao_user_srv.FrontUserRequest{Email:email,Code:captche,Password:password,Reassword:repassword})
				
			
			if err != nil {
				ctx.JSON(http.StatusOK,gin.H{
					"code":500,
					"msg":"注册失败",
				})
			}else {
				ctx.JSON(http.StatusOK,gin.H{
					"code":rep.Code,
					"msg":rep.Msg,
				})
			}

		}

	}else {
		ctx.JSON(http.StatusOK,gin.H{
			"code":500,
			"msg":"邮箱格式不正确",
		})
	}

}

func FrontUserLogin(ctx *gin.Context)  {

	mail := ctx.PostForm("mail")
	password := ctx.PostForm("password")

	fmt.Println("=======login========",mail,password)

	is_ok := utils.VerifyEmail(mail)

	if is_ok {


		// consulReg := consul.NewRegistry(
		// 	//指定微服务的ip:  选择注册服务器地址,默认为本机,也可以选择consul集群中的client
		// 	registry.Addrs("127.0.0.1:8500"),
		// )
		// // Create service
		// srv := micro.NewService(
		// 	//注册consul
		// 	micro.Registry(consulReg),
		// )
		// c := zhiliao_user_srv.NewFrontUserService("zhiliao.user.srv.zhiliao_user_srv", srv.Client())
		// rep, err := c.FrontUserLogin(context.TODO(),&zhiliao_user_srv.FrontUserRequest{
		// 		Email:mail,
		// 		Password:password,
		// 	})

			service := micro.NewService(
				micro.Client(grpc.NewClient()),
			)
			service.Init()
			client := zhiliao_user_srv.NewFrontUserService("zhiliao.user.srv.zhiliao_user_srv",service.Client())
			rep, err := client.FrontUserLogin(context.TODO(),&zhiliao_user_srv.FrontUserRequest{
				Email:mail,
				Password:password,
			})
	


		if err != nil {
			ctx.JSON(http.StatusOK,gin.H{
				"code":500,
				"msg":"用户名或密码错误",
			})
		}else {
			// 生成token
			tokenString,err2 := utils.GenToken(rep.UserName,utils.FrontUserExpireDuration,utils.FrontUserSecretKey)
			if err2 != nil {
				ctx.JSON(http.StatusOK,gin.H{
					"code":rep.Code,
					"msg":rep.Msg,
				})
			}else {

				ctx.JSON(http.StatusOK,gin.H{
					"code":rep.Code,
					"msg":rep.Msg,
					"token":tokenString,
					"username":rep.UserName,
				})
			}

		}
	}else {
		ctx.JSON(http.StatusOK,gin.H{
			"code":500,
			"msg":"邮箱地址不正确",
		})
	}
	// fmt.Println("========================login end======================",mail,password)
}